#ifndef _API_H_
#define _API_H_
#include <stdio.h>
#include <string.h>
#include "ctype.h"
#define DEBUG_LXJ
//#define ASN_READ_UINT8(buf)				(unsigned short)((unsigned short)(*(unsigned char*)(buf)
#define ASN_READ_UINT16(buf)			((unsigned short)((((unsigned short)(*(unsigned char*)(buf)))<<8) \
										|*((unsigned char*)(buf)+1)))
#define ASN_WRITE_UINT16(buf,word)		{*(unsigned char*)(buf) = (unsigned char)(((word)>>8)&0xFF); \
										 *((unsigned char*)(buf)+1) = (unsigned char)((word)&0xFF);}
#define ASN_READ_UINT24(buf)			(((unsigned long)(*((unsigned char*)(buf))<<16) \
										| (((unsigned long)(*((unsigned char*)(buf)+1)))<<8) \
										|*((unsigned char*)(buf)+2)))
#define ASN_WRITE_UINT24(buf, dword)	{*( (unsigned char*)(buf) = (unsigned char)((dword>>16)&0xFF);  \
										 *((unsigned char*)(buf + 1)) = (unsigned char)(((dword)>>8)&0xFF); \
										 *(((unsigned char*)(buf)+ 2)) = (unsigned char)((dword)&0xFF);}
#define ASN_READ_UINT32(buf)			((unsigned long)((((unsigned long)(*(unsigned char*)(buf)))<<24) \
										| (((unsigned long)(*((unsigned char*)(buf)+1)))<<16) \
										| (((unsigned long)(*((unsigned char*)(buf)+2)))<<8) \
										|*((unsigned char*)(buf)+3)))
#define ASN_WRITE_UINT32(buf, dword)	{*(unsigned char*)(buf) = (unsigned char)(((dword)>>24)&0xFF);  \
										 *( (unsigned char*)(buf)+1) = (unsigned char)((dword>>16)&0xFF);  \
										 *((unsigned char*)(buf+2)) = (unsigned char)(((dword)>>8)&0xFF); \
										 *(((unsigned char*)(buf)+3)) = (unsigned char)((dword)&0xFF);}


#define HAL_READ_UINT16(buf)			((unsigned short)((((unsigned short)(*((unsigned char*)(buf)+1)))<<8) \
										|*((unsigned char*)(buf))))
#define HAL_WRITE_UINT16(buf,word)		{*((unsigned char*)(buf) + 0) = (unsigned char)(((word)>>8)&0xFF); \
										 *((unsigned char*)(buf) + 1) = (unsigned char)((word)&0xFF);}
#define HAL_READ_UINT24(buf)			(((unsigned long)(*((unsigned char*)(buf)+2))<<16) \
										| (((unsigned long)(*((unsigned char*)(buf)+1)))<<8) \
										|*((unsigned char*)(buf))))
#define HAL_WRITE_UINT24(buf, dword)	{*(((unsigned char*)(buf) + 2)) = (unsigned char)((dword>>16)&0xFF);  \
										 *(((unsigned char*)(buf) + 1)) = (unsigned char)(((dword)>>8)&0xFF); \
										 *(((unsigned char*)(buf) + 0)) = (unsigned char)((dword)&0xFF);}
#define HAL_READ_UINT32(buf)			((unsigned long)((((unsigned long)(*((unsigned char*)(buf) + 3)))<<24) \
										| (((unsigned long)(*((unsigned char*)(buf) + 2)))<<16) \
										| (((unsigned long)(*((unsigned char*)(buf) + 1)))<<8) \
										|*((unsigned char*)(buf))))
#define HAL_WRITE_UINT32(buf, dword)	{*(((unsigned char*)(buf) + 3)) = (unsigned char)(((dword)>>24)&0xFF);  \
										 *(((unsigned char*)(buf) + 2)) = (unsigned char)((dword>>16)&0xFF);  \
										 *(((unsigned char*)(buf) + 1)) = (unsigned char)(((dword)>>8)&0xFF); \
										 *(((unsigned char*)(buf) + 0)) = (unsigned char)((dword)&0xFF);}

#define CMD_STB_START				0x01
#define CMD_MIXTERM_START			0x01
#define CMD_STB_DATA_REQUEST		0x02
#define CMD_STB_TIME_REQUEST		0x03
#define CMD_MIXTERM_TIME_REPLY		0x03
#define CMD_FILE_SIZE				0x04
#define CMD_REOPTION_LENGTH			0x07
#define CMD_MIXTERM_VER				0x09
#define CMD_DATA_MIXTERM_START		0x0A
#define CMD_DATA_ERROR				0x0B
#define CMD_DATA_TRANSFER_END		0x0C
#define CMD_MANUFACTURER_TYPE_HWVER 0x0D
#define CMD_STB_DL_OK				0x0E
#define CMD_STB_ERROR				0x0F
#define CMD_DeviceInfo_Request 	0x00B0

#define SOH                     0x01
#define STX						0x02
#define ETX						0x03

//#define MIXTERM_START_CMD    0x01
//#define MIXTERM_START_DATA	 0x0A
#define Irdeto_START_CMD	 0x01
#define Irdeto_START_DATA	 0x0A

#define MIXTERM_MODE_UPGRADE	0 
#define MIXTERM_MODE_WATCH		1 

#define PCMCIA_SLOT_DEVICE		0
#define UART_PORT_DEVICE		1
#define USB_DONGLE_DEVICE		0x1FFF
#define USB_CAM_DEVICE			0x100

 
int DownloadDataInit(char* filename);
void SetLoaderType(int dwLoaderType);
int RemoveDownloadData(void);
void ModuleWatchOneLine(void);
int SsuDownloadFileInit(void);
unsigned long GetDownloadData(char* pBuf, unsigned long offset, unsigned long length);
unsigned long GetDownloadDataLength(void);
void ShowDownloadSchedule(char bRate);
void GetDownloadErrorInfo(unsigned long dwErrorCode, char **pErrorName, char **pbDescription);
int SsuGetDii(unsigned char *pbDii, unsigned long *dwdpiiSize);
int SsuGetDdb(unsigned char *pbDdb, unsigned long *dwpDdbSize, unsigned short wModuleId, unsigned short wBlockNumber);
int ModuleReadCommand(unsigned char* pbcommand, unsigned long dwBufferSize, unsigned long *dwCommandSize);
int ModuleWriteCommand(const unsigned char* pbcommand, unsigned short dwCommandSize);

int Sas_PrintLog_Send(const unsigned char *send_buf, unsigned short len);
int Sas_Update_Read(unsigned char *read_buf, unsigned long buf_size, unsigned short *len);
#endif

